package com.qf.contract.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.FContract;
import com.qf.contract.entity.FContractDetails;
import com.qf.contract.entity.FSigningUnit;
import com.qf.contract.entity.FTypeOfContract;
import com.qf.contract.mapper.FContractMapper;
import com.qf.contract.qo.ContractApprovalQo;
import com.qf.contract.service.ContractApprovalService;
import com.qf.contract.vo.ContractApprovalVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 合同变更审批查询
 */
@Service
public class ContractApprovalServiceImpl implements ContractApprovalService {
    @Resource
    FContractMapper fContractMapper;

    /**
     * 待审批查询
     *
     * @param contractApprovalQo
     * @param current
     * @param size
     * @param num
     * @return
     */
    @Override
    public IPage<ContractApprovalVo> conditionalQuery(ContractApprovalQo contractApprovalQo, int current, int size, int num) {
        IPage<ContractApprovalVo> page;
        try {
            MPJLambdaWrapper<FContract> wrapper = new MPJLambdaWrapper<FContract>()
                    .selectAll(FContract.class)//查询user表全部字段
                    .selectAll(FContractDetails.class)
                    .selectAll(FTypeOfContract.class)
                    .selectAll(FSigningUnit.class)
                    .leftJoin(FContractDetails.class, FContractDetails::getContractId, FContract::getContractDetails)
                    .leftJoin(FTypeOfContract.class, FTypeOfContract::getContractId, FContract::getTypeOfContract)
                    .leftJoin(FSigningUnit.class, FSigningUnit::getUnitId, FContract::getSigningUnit);
            if (contractApprovalQo.getContractName() != null) {
                wrapper.eq("contract_name", contractApprovalQo.getContractName());
            }
            if (contractApprovalQo.getContractNumber() != null) {
                wrapper.eq("contract_number", contractApprovalQo.getContractNumber());
            }
            if (num == 0) {
                wrapper.ne("current_link", 3);
            } else {
                wrapper.ne("current_link", 1);
                wrapper.ne("current_link", 2);
                wrapper.ne("current_link", 0);
            }
            if (contractApprovalQo.getCrocessTitle() != null) {
                wrapper.like("contract_number", contractApprovalQo.getCrocessTitle())
                        .or().like("operator", contractApprovalQo.getCrocessTitle());
            }
            //TODO 分页连表查询
            page = fContractMapper.selectJoinPage(new Page<>(current, size), ContractApprovalVo.class, wrapper);
            //对查询的结果做处理
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return page;
    }

    /**
     * 已审批查询
     *
     * @param contractApprovalQo
     * @param current
     * @param size
     * @return
     */
    @Override
    public IPage<ContractApprovalVo> ContractApprovedQuery(ContractApprovalQo contractApprovalQo, int current, int size) {
        try {
            return conditionalQuery(contractApprovalQo, current, size, 1);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

}
